package com.example.tsbs.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.poi.ss.formula.functions.T;
import org.python.core.PyFunction;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.util.PythonInterpreter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @ClassName:  JythonUtils
 * @Description:TODO(jython 工具类)
 * @author: zhy
 * @Copyright: 2018 Inc. All rights reserved.
 * 注意：
 */
public class JythonUtils {

    private static Logger logger = LoggerFactory.getLogger(JythonUtils.class);

    /**
     * @Title: jythonInit
     * @Description: TODO(初始化jython)
     * @param: @return
     * @return: PythonInterpreter
     * @throws
     */
    public static PythonInterpreter jythonInit(String libPath){
        //初始化site 配置
        Properties props = new Properties();
        props.put("python.home", libPath); //python Lib 或 jython Lib,根据系统中该文件目录路径
        props.put("python.console.encoding", "UTF-8");
        props.put("python.security.respectJavaAccessibility", "false");
        props.put("python.import.site", "false");
        Properties preprops = System.getProperties();
        PythonInterpreter.initialize(preprops, props, new String[0]);
        //创建PythonInterpreter 对象
        PythonInterpreter interp = new PythonInterpreter();
        return interp;
    }

    /**
     * @Title: loadPythonFile
     * @Description: TODO(加载python 源码文件，)
     * @param: @param interp
     * @param: @param filePath ，比如：F:\\jpython_jar\\jpythonTest\\pythonTest.py  或/testpython/test.py
     * @param: @return
     * @return: PythonInterpreter
     * @throws
     */
    public static PythonInterpreter loadPythonFile(PythonInterpreter interp, String filePath){
        interp.execfile(filePath);
        return interp;
    }

    /**
     * @Title: loadPythonFunc
     * @Description: TODO(加载python 源码文件中的某个方法)
     * @param: @param interp
     * @param: @param functionName
     * @param: @return
     * @return: PyFunction
     * @throws
     */
    public static PyFunction loadPythonFunc(PythonInterpreter interp, String functionName){
        //加载方法
        PyFunction func = (PyFunction) interp.get(functionName,PyFunction.class);
        return func;
    }


    /**
     * @Title: execFunc
     * @Description: TODO(执行无参方法,返回PyObject)
     * @param: @param func
     * @return: PyObject
     * @throws
     */
    public static PyObject execFunc(PyFunction func){
        PyObject pyobj = func.__call__();
        return pyobj;
    }

    /**
     * @Title: execFuncToString
     * @Description: TODO(执行无参方法,返回一个字符串)
     * @param: @param func
     * @param: @return
     * @return: String
     * @throws
     */
    public static String execFuncToString(PyFunction func){
        PyObject pyobj = execFunc(func);
        return (String) pyobj.__tojava__(String.class);
    }

    /**
     * @Title: execFuncToString
     * @Description: TODO(执行有参方法,返回一个字符串)
     * @param: @param func
     * @param: @param paramName ，参数名
     * @param: @return
     * @return: String
     * @throws
     */
 /*public static String execFuncToString2(PyFunction func, JSONObject object){
	 String result = "";
	 if(object.get("voiceContent")!=null) {
		 if(object.get("voiceSender")!=null) {
			 func.__call__(new PyString("1"),new PyString("2"),new PyString("3"));
		 }
	 }
	  return null;
 }*/

    /**
     * @Title: execFuncToInteger
     * @Description: TODO(执行无参方法,返回一个Integer)
     * @param: @param func
     * @param: @return
     * @return: Integer
     * @throws
     */
    public Integer execFuncToInteger(PyFunction func){
        PyObject pyobj = execFunc(func);
        return (Integer) pyobj.__tojava__(Integer.class);
    }

    /**
     * @Title: execFuncToList
     * @Description: TODO(执行无参方法,返回一个List)
     * @param: @param func
     * @param: @return
     * @return: List<T>
     * @throws
     */
    @SuppressWarnings("unchecked")
    public List<T> execFuncToList(PyFunction func){
        PyObject pyobj = execFunc(func);
        return (List<T>) pyobj.__tojava__(List.class);
    }

    /**
     * @Title: execFuncToMap
     * @Description: TODO(执行无参方法,返回一个Map<String, Object>)
     * @param: @param func
     * @param: @return
     * @return: Map<String,Object>
     * @throws
     */
    @SuppressWarnings("unchecked")
    public Map<String, Object> execFuncToMap(PyFunction func){
        PyObject pyobj = execFunc(func);
        return (Map<String, Object>) pyobj.__tojava__(Map.class);
    }

    public void execFuncToByParamsList(PyFunction func, List<T> paramsList){

    }

    public static int sendVoiceByJava(JSONObject object, String alertNum) {
        int re = 1;
        try {
            if(object.get("pythonMethod")==null||object.get("pythonLib")==null||object.get("voiceScript")==null) {
                logger.error("当前配置的python脚本参数错误,请先检查参数.................");
            }else {
                List<String> paramLst = new ArrayList<String>();
                paramLst.add("python");//
                paramLst.add((String)object.get("voiceScript"));
                paramLst.add(object.get("voiceRecevier")==null?null:object.getString("voiceRecevier"));
                if(object.get("voiceContent")!=null&&object.get("voiceKeys")!=null) {
                    String[] params = object.getString("voiceKeys").split(",", -1);
                    for(int i = 0;i<params.length;i++) {
                        JSONArray parseArray = JSON.parseArray(object.getString("voiceContent"));
                        if(parseArray.getJSONObject(i).get(params[i])==null) {
                            paramLst.add(null);
                        }else {
                            paramLst.add(parseArray.getJSONObject(i).getString(params[i]));
                        }
                    }
                    logger.info("Python Params Are: "+paramLst.toString());
                    Process process = Runtime.getRuntime().exec((String[])paramLst.toArray());
                    re = process.waitFor();
                }
            }
        } catch (Exception e) {
            logger.error("Python脚本调用异常............................", e);
            re = 2;
        }
        return re;

    }




    public static boolean sendVoiceByJython(JSONObject object, String alertNum) {
        boolean flag = false;
        try {
            if(object.get("pythonMethod")==null||object.get("pythonLib")==null||object.get("voiceScript")==null) {
                logger.error("当前配置的python脚本参数错误,请先检查参数.................");
            }else {
                PythonInterpreter interp  = jythonInit(object.getString("pythonLib"));
                //获取py脚本路径
                interp = loadPythonFile(interp,object.getString("voiceScript"));
                //设置参数
                //interp.set("voiceSender", object.get("voiceSender")==null?null:object.getString("voiceSender"));
                interp.set("voiceRecevier", object.get("voiceRecevier")==null?null:object.getString("voiceRecevier"));
                if(object.get("voiceContent")!=null&&object.get("voiceKeys")!=null) {
                    String[] params = object.getString("voiceKeys").split(",", -1);
                    for(int i = 0;i<params.length;i++) {
                        JSONArray parseArray = JSON.parseArray(object.getString("voiceContent"));
                        if(parseArray.getJSONObject(i).get(params[i])==null) {
                            interp.set(params[i], null);
                        }else {
                            interp.set(params[i], parseArray.getJSONObject(i).getString(params[i]));
                        }
                    }
                    PyFunction func = loadPythonFunc(interp, object.getString("pythonMethod"));
                    PyObject pyObj = func.__call__();
                    flag =  (boolean) pyObj.__tojava__(Boolean.class);
                }
            }
        } catch (Exception e) {
            logger.error("Python脚本调用异常............................", e);
        }

        return flag;
    }

    public static void main(String[] args) {

        //入参只能填4个
        PythonInterpreter interpreter =  jythonInit("D:\\Devlop\\python");
        interpreter.execfile("C:\\Users\\zhonghy\\Desktop\\zhonghy\\demo.py");
        PyFunction function = (PyFunction)interpreter.get("my_test",PyFunction.class);
        PyObject pyobject = function.__call__(new PyString("huzhiwei"),new PyString("25"));
        System.out.println("anwser = " + pyobject.toString());

/*   try {
    	 String[] params = new String[] {"python","C:\\Users\\zhonghy\\Desktop\\zhonghy\\demo.py","10","20"};
         Process proc=Runtime.getRuntime().exec(params); //执行py文件
         InputStreamReader stdin=new InputStreamReader(proc.getInputStream());
         LineNumberReader input=new LineNumberReader(stdin);
         String line;
         while((line=input.readLine())!=null ){
             System.out.println(line);//得到输出
         }
         int re = proc.waitFor();
         System.out.println(re);
     } catch (Exception e) {
         e.printStackTrace();
     }*/
    }


}
